package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

/*@Transactional 是Spring提供的一个用于管理事物的注解，用来管理类或方法上的事务行为
* 在岁数据库做操作时，可以确保方法中的所有数据操作都在同一事务中执行，要么都成功，要么都失败*/
@Transactional
@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    AuditService auditService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AuditMapper auditMapper;

    @Override
    public void saveApplication(ApplicationSaveParam applicationSaveParam) {
        log.debug("保存申请单业务:{}", applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam, application);
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());
       /* 遇到的问题:新增申请单对应的审批单时,审批单数据没有此申请单id
        原因:执行insert方法的SQL时,并没有把刚刚生成的申请单id回填到application对象中
        解决办法:给此SQL上加useGeneratedKeys="true" keyProperty="id"属性
        效果:JDBC自动回填此申请单id到application对象的id属性中,再传给审批,审批就拿到申请单id了*/
        applicationMapper.insert(application);

        //还需要为当前申请单生成对应的审批单数据
        auditService.insertAudit(application);
    }

    @Override
    public void cancel(Long id) {
        log.debug("撤销申请单业务:{}", id);
        Application application = new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);

        //还需要将此申请单对应的所有审批单删除!!!
        auditMapper.deleteByApplicationId(id);
    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        log.debug("查询申请单业务:{}", applicationQuery);
        List<ApplicationVO> list = applicationMapper.selectApplication(applicationQuery);
        //遍历每一个申请单VO,为其补全审批人数据
        for (int i = 0; i < list.size(); i++) {
            ApplicationVO applicationVO = list.get(i);
            //处理当前遍历到的这个申请单VO,补全数据
            assignAuditUserList(applicationVO);
        }
        return list;
    }

    // List<Long> auditUserIdList;//审批人id集合 [106,103]
    // String auditUsernameList;//审批人姓名字符串 "moly,tom"
    private void assignAuditUserList(ApplicationVO applicationVO) {
        //1.准备空集合,用来存放查到的审批人数据
        List<Long> auditUserIdList = new ArrayList<>();
        List<String> auditUsernameList = new ArrayList<>();
        //2.获取当前申请单id,查询此申请单对应的所有审批单VO
        List<AuditVO> auditVOList =
                auditService.selectAuditByApplicationId(applicationVO.getId());
        //3.遍历取出每一个审批单vo
        for(int i = 0; i < auditVOList.size(); i++){
            AuditVO auditVO = auditVOList.get(i);
            //4.获取当前审批VO中的审批人信息
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);
            //5.继续根据审批人id查出审批人姓名
            UserVO userVO = userMapper.selectById(id);
            auditUsernameList.add(userVO.getUsername());
        }
        //6.准备一个拼接工具
        StringJoiner stringJoiner = new StringJoiner(",");
        //7.依次将每个审批人姓名存入拼接工具中
        for(String username : auditUsernameList){
            stringJoiner.add(username);
        }
        //8.将我们准备好的值赋值给申请VO缺失值的字段
        applicationVO.setAuditUserIdList(auditUserIdList);
        applicationVO.setAuditUsernameList(stringJoiner.toString());
    }
}
